import { createRouter, createWebHistory } from 'vue-router';
import Register from '../components/user/Register.vue'
import Login from '../components/user/Login.vue'
import Home from '../components/home/Home.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    // 注意，这里的name不能重名，否则首页会失效
    { path: '/', name: 'Index', component: Login,
      // beforeEnter: (to, from, next) => {
      //   if (!isLoggedIn()) {
      //     next('/login');
      //   } else {
      //     next('/home')
      //   }
      // }
    },
    { path: '/login', name: 'Login', component: Login },
    { path: '/register', name: 'Register', component: Register },
    { path: '/home', name: 'Home', component: Home}
  ]
})
function isLoggedIn() {
  // 这里可以检查用户的登录状态，例如从 localStorage 或 Vuex 获取
  return localStorage.getItem('isLogin') === 'true';
}
export default router